package lar.system.network;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import lar.system.engine.MainProgram;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

public class UDPSender 
{
	private int port;
	private DatagramSocket outSocket;
	private InetAddress targetAddress;
	
	public UDPSender(InetAddress target, int destPort) throws IOException
	{
		outSocket = new DatagramSocket();
        outSocket.setBroadcast(true);
        outSocket.setReuseAddress(true);
        targetAddress = target;
        this.port = destPort;
	}
	
	public UDPSender(InetAddress target, int srcPort, int destPort) throws IOException
	{
		outSocket = new DatagramSocket(srcPort);
        outSocket.setBroadcast(true);
        outSocket.setReuseAddress(true);
        targetAddress = target;
        this.port = destPort;
	}

	
	public void shutDown()
	{
		if(outSocket == null) return;
		outSocket.close();
		outSocket = null;
	}
	
	public void send(String msg)
	{
		if(outSocket == null) return;
		DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(), msg.length(), targetAddress, port);
        try 
        {
			outSocket.send(datagramPacket);
			//Log.d("R", "Sent Broadcast: " + msg);
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
